﻿using Microsoft.VisualBasic;
using RBAC.Domain;
using RBAC.Repository.RabbitInterfae;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceab.DTO;
using Traceability.Domain;
using Traceability.Service.Interfae;


namespace Traceability.Service.Impl
{
    public class TraceabilitysService : ITraceabilitysService
    {
        private readonly ITraceabilitysRepository _Trepository;
        private readonly IGoodsRepository _goodsRepository;
        private readonly IImgRepository _imgRepository;
        public TraceabilitysService(ITraceabilitysRepository trepository, IGoodsRepository goodsRepository,
            IImgRepository imgRepository)
        {
            _Trepository = trepository;
            _goodsRepository = goodsRepository;
            _imgRepository = imgRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="traceabilitys"></param>
        /// <returns></returns>

        public async Task<int> AddTraceabilitys(Traceabilitys traceabilitys)
        {
           return await _Trepository.AddAsync(traceabilitys);
        }
        /// <summary>
        /// 单删
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelTra(int id)
        {
            var del = await _Trepository.GetModelsAsync(id);
            del.TraceabilityDel = true;
            return await _Trepository.UpdateAsync(del);
            
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DelTraceabilitys(List<int> ids)
        {
            var dellist = await _Trepository.GetAllAsync();
            dellist = dellist.ToList().Where(d => ids.Contains(d.TraceabilityId)).ToList();
            foreach (var item in dellist)
            {
                item.TraceabilityDel = true;
                item.delName = "王宏";
                item.delDate = DateTime.Now;
              
            }
            return await _Trepository.UpdateRange(dellist);


        }
        /// <summary>
        /// 显示全部
        /// </summary>
        /// <returns></returns>
        public async Task<PageDto<TraceDto>> GetTraceabilitys(int pageSize, int pageIndex)
        {
            PageDto<TraceDto> pageDto = new PageDto<TraceDto>();
            var listobj = from a in await _Trepository.GetAllAsync()
                       join b in await _goodsRepository.GetAllAsync()
                       on a.GoodsdId equals b.GoodsdId
                       
                       where !a.TraceabilityDel
                       select new TraceDto
                       {
                         
                           GoodsdId = b.GoodsdId,
                           GoodsdName = b.GoodsdName,
                           TraceabilityId = a.TraceabilityId,
                           Batch = a.Batch,
                           License = a.License,
                           Sellby = a.Sellby,
                           ManufacturingEnterprise = a.ManufacturingEnterprise,
                           TraceabilityDel = a.TraceabilityDel,
                           addName = a.addName,
                           addDate = a.addDate,
                           updateName = a.updateName,
                           updateDate = a.updateDate,
                           delName = a.delName,
                           delDate = a.delDate,
                       };

            pageDto.ToalCount = listobj.Count();
            pageDto.PageCount = (int)Math.Ceiling(listobj.Count() * 1.0 / pageSize);
            pageDto.list = listobj.OrderByDescending(d => d.addDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageDto;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Traceabilitys> GetTraceabilitysByid(int id)
        {
           return await _Trepository.GetModelsAsync(id);
        }
        /// <summary>
        /// 详情反填
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public async Task<List<TraceDto>> GetTraceabilitysBy(string batchs)
        {
            var listobj = from a in await _Trepository.GetAllAsync()
                          join b in await _goodsRepository.GetAllAsync()
                          on a.GoodsdId equals b.GoodsdId

                          where a.Batch==batchs
                          select new TraceDto
                          {

                              GoodsdId = b.GoodsdId,
                              GoodsdName = b.GoodsdName,
                              TraceabilityId = a.TraceabilityId,
                              Batch = a.Batch,
                              License = a.License,
                              Sellby = a.Sellby,
                              ManufacturingEnterprise = a.ManufacturingEnterprise,
                              Imgs=a.Imgs,
                              TraceabilityDel = a.TraceabilityDel,
                              addName = a.addName,
                              addDate = a.addDate,
                              updateName = a.updateName,
                              updateDate = a.updateDate,
                              delName = a.delName,
                              delDate = a.delDate,
                          };
            return listobj.ToList();


        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="traceabilitys"></param>
        /// <returns></returns>
        public async Task<int> UpdateTraceabilitys(Traceabilitys traceabilitys)
        {
          
            return await _Trepository.UpdateAsync(traceabilitys);
        }
    }
}
